<template>
  <div v-if="visible" class="modal-overlay" @click.self="close">
    <div class="modal">
      <span class="modal-close-button" @click="close">X</span>
      <h2>登录</h2>
      <form @submit.prevent="handleSubmit">
        <div class="input-group">
          <input 
            type="text" 
            v-model="formData.username" 
            placeholder="请输入用户名"
            required
          />
        </div>
        <div class="input-group">
          <input 
            :type="showPassword ? 'text' : 'password'" 
            v-model="formData.password"
            placeholder="请输入密码" 
            required
          />
          <el-icon class="toggle-password" @click="togglePasswordVisibility">
            <component :is="showPassword ? 'Hide' : 'View'"/>
          </el-icon>
        </div>
        <button type="submit" class="submit-button">登录</button>
      </form>
      <div class="login-options">
        <span class="admin-entry" @click="navigateToAdminLogin">
          管理员入口
        </span>
        <span class="register-prompt" @click="showRegisterForm">
          没有账号？点击注册
        </span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, defineProps, defineEmits } from 'vue'
import { ElIcon } from 'element-plus'
import { View, Hide } from '@element-plus/icons-vue'
import axios from 'axios'
import { useRouter } from 'vue-router'

const router = useRouter()

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits([
  'close',
  'login-success',
  'show-register'
])

const formData = ref({
  username: '',
  password: ''
})
const showPassword = ref(false)

const togglePasswordVisibility = () => {
  showPassword.value = !showPassword.value
}

const close = () => {
  emit('close')
}

const navigateToAdminLogin = () => {
  const routeData = router.resolve({ path: '/admin/login' })
  window.open(routeData.href, '_blank')
}

const showRegisterForm = () => {
  emit('show-register')
}

const handleSubmit = async () => {
  try {
    const response = await axios.post('http://localhost:8092/user/login', {
      username: formData.value.username,
      password: formData.value.password
    })

    if (response.data.token) {
      localStorage.setItem('token', response.data.token)
      axios.defaults.headers.common['Authorization'] = `Bearer ${response.data.token}`
      emit('login-success')
      close()
    }
  } catch (error) {
    console.error('登录失败:', error)
    if (error.response?.data?.error) {
      alert(error.response.data.error)
    }
  }
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal {
  background-color: white;
  padding: 30px;
  border-radius: 8px;
  width: 400px;
  height: auto;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: relative;
}

.modal-close-button {
  position: absolute;
  top: 10px;
  right: 10px;
  font-size: 24px;
  cursor: pointer;
  color: #666;
}

.input-group {
  margin-bottom: 20px;
  position: relative;
}

input[type="text"],
input[type="password"] {
  width: 100%;
  padding: 10px;
  padding-right: 40px;
  border: 1px solid #ccc;
  border-radius: 5px;
  font-size: 16px;
  box-sizing: border-box;
}

.toggle-password {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  font-size: 18px;
  color: #666;
}

.submit-button {
  background-color: #4caf50;
  color: white;
  border: none;
  padding: 12px 20px;
  cursor: pointer;
  border-radius: 5px;
  width: 100%;
  font-size: 16px;
  transition: background-color 0.3s;
}

.submit-button:hover {
  background-color: #45a049;
}

.login-options {
  display: flex;
  justify-content: space-between;
  margin-top: 15px;
}

.admin-entry,
.register-prompt {
  color: #007BFF;
  cursor: pointer;
  font-size: 14px;
  transition: color 0.3s;
}

.admin-entry:hover,
.register-prompt:hover {
  color: #0056b3;
  text-decoration: underline;
}
</style>